home *** CD-ROM | disk | FTP | other *** search
Text File | 1991-11-04 | 3.5 KB | 140 lines | [TEXT/MPS ] |
- /*
- Apple Macintosh Developer Technical Support,
- TRandom 1.0 - Saturday, October 5, 1991 18:26:35
-
- TRandom.h - C++ source
-
- Copyright © 1991 Apple Computer, Inc.
- All rights reserved.
-
- TRandom is a stackbased utility class for random number generation.
- TRandom.h contains the header file information for the class.
- */
-
-
-
- // include it only once
- #pragma once
-
- // INCLUDE FILES
- #include <Types.h>
- #include <OSUtils.h>
- #include <Quickdraw.h>
- #include <Events.h>
- #include <CursorCtl.h>
- #include <stream.h>
- #include <stdlib.h>
-
-
- // EXTERN FUNCTIONS
- extern "C"{
- pascal short AsmRandom();
- };
-
-
- // CLASS INTERFACE
-
- class TRandom { // Random number class
- public:
- // TYPEDEFS AND ENUMS
- enum eRandType {MACOS,QUICK,SHUFFLE,PM}; // selected random number generator
-
- // CONSTRUCTORS
- TRandom(eRandType = MACOS, long theSeed = 1,// default constructor
- unsigned short low = 0, unsigned short high = 100);
- TRandom(const TRandom&); // copy constructor
- TRandom& operator=(const TRandom&); // assignment operator
- virtual ~TRandom(); // virtual destructor
-
- // MAIN INTERFACES
- unsigned short Next(); // get next random number
- TRandom& ShuffleSeed(); // set seed value
- TRandom& SetRandomGenerator(eRandType); // select random number algorithm
-
- // PUBLIC ACCESSORS AND MUTATORS
- long GetSeedValue() const;
- TRandom& SetSeedValue(const long theSeed);
- eRandType GetAlgorithmType() const;
-
- // TESTING
- static void TestRandomClass(); // static function with test code
- static void CalcTime(const char*); // calculate time during test
-
- protected:
- // INITIATION ROUTINES
- Boolean IRandom(eRandType); // initialize needed class information
-
- // RANDOM GENERATOR ALGORITHMS/MEMBER FUNCTIONS
- unsigned short MacRandom(); // Toolbox Random function
- unsigned short QuickRandom(); // quick assembly version
- unsigned short ShuffleRandom(); // shuffle Toolbox random values
- TRandom& InitShuffleRandom(); // initialize the shuffle algorithm
- unsigned short ParkMiller(); // Park&Miller Random generator
-
-
- private:
- // TYPEDEFS AND ENUMS (INTERNAL IMPLEMENTATION)
- enum eSIZE { eSHUFFLETABLE = 128, ePM1 = 2836, ePM2 = 16807, e16BIT = 65536,
- ePM3 = 127773, eMSEED = 1618003398, eBIG = 1000000000,
- eACM_MAX = 2147483647 };
-
- typedef unsigned short (TRandom::*RandGen)();
- // ptr to actual selected random routine
-
- // FIELDS
- RandGen fGenerator; // selected random generator function
- eRandType fAlgorithm; // selected algorithm
- unsigned short fLow; // lower limit of random number
- unsigned short fHigh; // higher limit of random number
- unsigned short fRange; // max size random number
- long fSeed; // random generator seed
- Boolean fState; // current state of object, OK or BAD
- unsigned short fPrevNum; // previous random number
- unsigned short fShuffleBuf[eSHUFFLETABLE]; // buffer for the shuffle algorithm
- };
-
-
- // INLINE METHODS
-
- inline unsigned short
- TRandom::Next()
- {
- return (this->*fGenerator)();
- }
-
- inline TRandom&
- TRandom::ShuffleSeed()
- {
- this->fSeed = TickCount();
- return *this;
- }
-
- inline TRandom&
- TRandom::SetRandomGenerator(eRandType theType)
- {
- this->IRandom(theType);
- return *this;
- }
-
- inline long
- TRandom::GetSeedValue() const
- {
- return this->fSeed;
- }
-
- inline TRandom&
- TRandom::SetSeedValue(const long theSeed)
- {
- this->fSeed = theSeed;
- qd.randSeed = theSeed;
- return *this;
- }
-
-
- inline TRandom::eRandType
- TRandom::GetAlgorithmType() const
- {
- return this->fAlgorithm;
- }
-
-